home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 22 / PC Actual CD 22.iso / progs / DIRECTOR / data.z / Learning Director.dcr / UtilsCast_5_PS_ListScroller.ls < prev    next >
Encoding:
Text File  |  1997-05-10  |  10.1 KB  |  360 lines

  1. property pTextLists, pClickCell, pClickHandler, pHasControls, pDrawingEnabled, pVScrollSpriteNum, pVThumbSpriteNum, pUpArrowSpriteNum, pDownArrowSpriteNum, pTotalVisLines, pCanScroll, pListTopV, pLineHeight, pTopLine, pHiliteFlag, pThumbTravel, pThumbTravelBottom, pThumbIncrement, pSelectedLine, pInitialized
  2.  
  3. on birth me, vScrollSpriteNum, vThumbSpriteNum, upArrowSpriteNum, downArrowSpriteNum, textLists, clickHandler
  4.   return new(me, vScrollSpriteNum, vThumbSpriteNum, upArrowSpriteNum, downArrowSpriteNum, textLists, clickHandler)
  5. end
  6.  
  7. on new me, vScrollSpriteNum, vThumbSpriteNum, upArrowSpriteNum, downArrowSpriteNum, textLists, clickHandler
  8.   set pVScrollSpriteNum to vScrollSpriteNum
  9.   set pVThumbSpriteNum to vThumbSpriteNum
  10.   set pUpArrowSpriteNum to upArrowSpriteNum
  11.   set pDownArrowSpriteNum to downArrowSpriteNum
  12.   set pHasControls to pVScrollSpriteNum <> 0
  13.   if not pHasControls then
  14.     set pNoControls to 1
  15.     set pVThumbSpriteNum to 0
  16.     set pUpArrowSpriteNum to 0
  17.     set pDownArrowSpriteNum to 0
  18.   end if
  19.   set pDrawingEnabled to 1
  20.   set pHiliteFlag to 1
  21.   set pTextLists to textLists
  22.   if voidp(clickHandler) then
  23.     set pClickHandler to EMPTY
  24.   else
  25.     set pClickHandler to clickHandler
  26.   end if
  27.   set pClickCell to point(0, 0)
  28.   if pHasControls then
  29.     puppetSprite(pVThumbSpriteNum, 1)
  30.     puppetSprite(pUpArrowSpriteNum, 1)
  31.     puppetSprite(pDownArrowSpriteNum, 1)
  32.     set the locH of sprite pVThumbSpriteNum to the left of sprite pVScrollSpriteNum
  33.   end if
  34.   set firstTextSprite to getPropAt(pTextLists, 1)
  35.   set pListTopV to the top of sprite firstTextSprite
  36.   set pLineHeight to the textHeight of field the castNum of sprite firstTextSprite
  37.   set pTotalVisLines to the height of sprite firstTextSprite / pLineHeight
  38.   if pHasControls then
  39.     set pThumbTravel to the top of sprite pDownArrowSpriteNum - the bottom of sprite pUpArrowSpriteNum - the height of sprite pVThumbSpriteNum
  40.     set pThumbTravelBottom to the bottom of sprite pUpArrowSpriteNum + pThumbTravel
  41.   else
  42.     set pThumbTravel to 0
  43.     set pThumbTravelBottom to 0
  44.   end if
  45.   set pSelectedLine to 0
  46.   set pTopLine to 0
  47.   set pInitialized to 0
  48.   updateContents(me)
  49.   return me
  50. end
  51.  
  52. on ableDrawing me, able
  53.   set pDrawingEnabled to able
  54.   if not able then
  55.     set pInitialized to 0
  56.   end if
  57. end
  58.  
  59. on showTextAt me, topLine
  60.   if (topLine < 0) or not pInitialized then
  61.     set forceRefresh to 1
  62.     set topLine to abs(topLine)
  63.   else
  64.     set forceRefresh to 0
  65.   end if
  66.   set numLists to count(pTextLists)
  67.   set listLines to count(getAt(pTextLists, 1))
  68.   if topLine > listLines then
  69.     set topLine to listLines
  70.   else
  71.     if topLine < 1 then
  72.       set topLine to 1
  73.     end if
  74.   end if
  75.   if not pDrawingEnabled then
  76.     set pTopLine to topLine
  77.     return 
  78.   end if
  79.   set the updateLock to 1
  80.   if (pTopLine <> topLine) or forceRefresh then
  81.     set oldTopLine to pTopLine
  82.     set pTopLine to topLine
  83.     set botLine to topLine + pTotalVisLines - 1
  84.     if botLine > listLines then
  85.       set botLine to listLines
  86.     end if
  87.     set rebuildLinesNeeded to (oldTopLine <> topLine) or not pInitialized
  88.     repeat with listIndex = 1 to numLists
  89.       set charOffsetList to [1]
  90.       set spriteNum to getPropAt(pTextLists, listIndex)
  91.       set textCastNum to the castNum of sprite spriteNum
  92.       set textList to getAt(pTextLists, listIndex)
  93.       set newTextLines to EMPTY
  94.       if listLines > 0 then
  95.         if rebuildLinesNeeded or pHiliteFlag then
  96.           repeat with lineNum = topLine to botLine
  97.             put getAt(textList, lineNum) after newTextLines
  98.             if lineNum < botLine then
  99.               put RETURN after newTextLines
  100.             end if
  101.             append(charOffsetList, length(newTextLines) + 1)
  102.           end repeat
  103.         end if
  104.         if rebuildLinesNeeded then
  105.           set the text of field textCastNum to newTextLines
  106.           startTimer()
  107.           repeat while the timer < 20
  108.             nothing()
  109.           end repeat
  110.         end if
  111.         if pHiliteFlag then
  112.           if (pSelectedLine >= topLine) and (pSelectedLine <= botLine) then
  113.             set lineToSelect to pSelectedLine - topLine + 1
  114.             hilite char getAt(charOffsetList, lineToSelect) to getAt(charOffsetList, lineToSelect + 1) - 1 of field textCastNum
  115.           end if
  116.         end if
  117.         next repeat
  118.       end if
  119.       set the text of field textCastNum to newTextLines
  120.     end repeat
  121.   end if
  122.   set pInitialized to 1
  123.   if pHasControls then
  124.     set thumbV to the bottom of sprite pUpArrowSpriteNum
  125.     set thumbV to thumbV + (pThumbIncrement * (pTopLine - 1))
  126.     set the locV of sprite pVThumbSpriteNum to thumbV
  127.   end if
  128.   set the updateLock to 0
  129.   updateStage()
  130. end
  131.  
  132. on scrollUp1 me
  133.   showTextAt(me, pTopLine - 1)
  134. end
  135.  
  136. on scrollDown1 me
  137.   showTextAt(me, pTopLine + 1)
  138. end
  139.  
  140. on updateContents me
  141.   set listLines to count(getAt(pTextLists, 1))
  142.   if (pTopLine = 0) or (pTopLine > listLines) then
  143.     set pTopLine to 1
  144.   end if
  145.   if pHasControls then
  146.     set pCanScroll to listLines > pTotalVisLines
  147.     set the visible of sprite pVThumbSpriteNum to pCanScroll
  148.     if listLines > 1 then
  149.       set pThumbIncrement to float(pThumbTravel) / (listLines - 1)
  150.     else
  151.       set pThumbIncrement to 0
  152.     end if
  153.     set thumbV to the bottom of sprite pUpArrowSpriteNum
  154.     set thumbV to thumbV + (pThumbIncrement * (pTopLine - 1))
  155.     set the locV of sprite pVThumbSpriteNum to thumbV
  156.   end if
  157.   showTextAt(me, -pTopLine)
  158.   if not pCanScroll then
  159.     set col1Sprite to getPropAt(pTextLists, 1)
  160.     set clipHeight to the height of sprite col1Sprite
  161.     set textHeight to the height of member the castNum of sprite col1Sprite
  162.     set pCanScroll to textHeight > clipHeight
  163.     set the visible of sprite pVThumbSpriteNum to pCanScroll
  164.   end if
  165. end
  166.  
  167. on inUpArrow me
  168.   if not pCanScroll then
  169.     exit
  170.   end if
  171.   if pHasControls then
  172.     puppetSprite(pUpArrowSpriteNum, 1)
  173.     set aCastNum to the castNum of sprite pUpArrowSpriteNum
  174.     set the castNum of sprite pUpArrowSpriteNum to aCastNum + 1
  175.     updateStage()
  176.   end if
  177.   repeat while the mouseDown
  178.     scrollUp1(me)
  179.   end repeat
  180.   if pHasControls then
  181.     set the castNum of sprite pUpArrowSpriteNum to the number of member aCastNum
  182.     updateStage()
  183.     puppetSprite(pUpArrowSpriteNum, 0)
  184.   end if
  185. end
  186.  
  187. on inDownArrow me
  188.   if not pCanScroll then
  189.     exit
  190.   end if
  191.   if pHasControls then
  192.     puppetSprite(pDownArrowSpriteNum, 1)
  193.     set aCastNum to the castNum of sprite pDownArrowSpriteNum
  194.     set the castNum of sprite pDownArrowSpriteNum to aCastNum + 1
  195.     updateStage()
  196.   end if
  197.   repeat while the mouseDown
  198.     scrollDown1(me)
  199.   end repeat
  200.   if pHasControls then
  201.     set the castNum of sprite pDownArrowSpriteNum to the number of member aCastNum
  202.     updateStage()
  203.     puppetSprite(pDownArrowSpriteNum, 0)
  204.   end if
  205. end
  206.  
  207. on inThumb me
  208.   if pHasControls then
  209.     if not pCanScroll then
  210.       exit
  211.     end if
  212.     repeat while the mouseDown
  213.       set thumbV to constrainV(pVScrollSpriteNum, the mouseV)
  214.       if thumbV > pThumbTravelBottom then
  215.         set thumbV to pThumbTravelBottom
  216.       end if
  217.       set the locV of sprite pVThumbSpriteNum to thumbV
  218.       updateStage()
  219.     end repeat
  220.     set newTopLine to ((thumbV - the top of sprite pVScrollSpriteNum) / pThumbIncrement) + 1
  221.     set newTopLine to integer(newTopLine)
  222.     showTextAt(me, newTopLine)
  223.   end if
  224. end
  225.  
  226. on inScrollBar me
  227.   if pHasControls then
  228.     if not pCanScroll then
  229.       exit
  230.     end if
  231.     set clickPosV to the mouseV
  232.     if clickPosV < the top of sprite pVThumbSpriteNum then
  233.       showTextAt(me, pTopLine - pTotalVisLines)
  234.     else
  235.       if clickPosV > the bottom of sprite pVThumbSpriteNum then
  236.         showTextAt(me, pTopLine + pTotalVisLines)
  237.       end if
  238.     end if
  239.   end if
  240. end
  241.  
  242. on inText me
  243.   set clickPosV to the mouseV
  244.   set visLineNum to the mouseLine - 1
  245.   if visLineNum < 0 then
  246.     return 0
  247.   end if
  248.   set selLineNum to visLineNum + pTopLine
  249.   selectLine(me, selLineNum, 0)
  250.   set N to count(pTextLists)
  251.   repeat with col = 1 to N
  252.     set spriteNum to getPropAt(pTextLists, col)
  253.     if inside(point(the mouseH, the mouseV), the rect of sprite spriteNum) then
  254.       exit repeat
  255.     end if
  256.   end repeat
  257.   set pClickCell to point(selLineNum, col)
  258.   if pClickHandler <> EMPTY then
  259.     set s to pClickHandler & "( me, pClickCell )"
  260.     do(s)
  261.   end if
  262.   return 1
  263. end
  264.  
  265. on getClickCell me
  266.   return the pClickCell of me
  267. end
  268.  
  269. on selectLine me, selLineNum, scrollToLine
  270.   if (selLineNum <> pSelectedLine) or scrollToLine then
  271.     set listLines to count(getAt(pTextLists, 1))
  272.     if selLineNum > listLines then
  273.       set pSelectedLine to listLines
  274.     else
  275.       set pSelectedLine to max(selLineNum, 1)
  276.     end if
  277.     if the pCanScroll of me and scrollToLine then
  278.       set topLine to pSelectedLine
  279.     else
  280.       set topLine to pTopLine
  281.     end if
  282.     showTextAt(me, -topLine)
  283.   end if
  284. end
  285.  
  286. on getSelectedCell me, columnNum
  287.   if pSelectedLine > 0 then
  288.     return getAt(getAt(pTextLists, columnNum), pSelectedLine)
  289.   else
  290.     return EMPTY
  291.   end if
  292. end
  293.  
  294. on setSelectedCell me, columnNum, text
  295.   if pSelectedLine > 0 then
  296.     set columnText to getAt(pTextLists, columnNum)
  297.     setAt(columnText, pSelectedLine, text)
  298.     set spriteNum to getPropAt(pTextLists, columnNum)
  299.     set textCastNum to the castNum of sprite spriteNum
  300.     set lineNum to pSelectedLine - pTopLine + 1
  301.     put text into line lineNum of field textCastNum
  302.     if pHiliteFlag then
  303.       set the updateLock to 1
  304.       put text into line lineNum of field textCastNum
  305.       showTextAt(me, -pTopLine)
  306.       set the updateLock to 0
  307.     else
  308.       put text into line lineNum of field textCastNum
  309.     end if
  310.   end if
  311. end
  312.  
  313. on getSelectedLine me
  314.   if pSelectedLine > 0 then
  315.     return pSelectedLine
  316.   else
  317.     return 0
  318.   end if
  319. end
  320.  
  321. on getColumnText me, columnNum
  322.   return getAt(pTextLists, columnNum)
  323. end
  324.  
  325. on setColumnText me, columnNum, textList
  326.   setAt(pTextLists, columnNum, textList)
  327.   set pInitialized to 0
  328. end
  329.  
  330. on dataChanged me
  331.   set pTopLine to 0
  332.   set pInitialized to 0
  333.   updateContents(me)
  334.   updateContents(me)
  335. end
  336.  
  337. on getNumVisibleLines me
  338.   return pTotalVisLines
  339. end
  340.  
  341. on isLineVisible me, lineNum
  342.   if (lineNum < pTopLine) or (lineNum > (pTopLine + pTotalVisLines - 1)) then
  343.     return 0
  344.   else
  345.     return 1
  346.   end if
  347. end
  348.  
  349. on getTopLine me
  350.   return pTopLine
  351. end
  352.  
  353. on getTotalLines me
  354.   return count(getAt(pTextLists, 1))
  355. end
  356.  
  357. on setHiliteFlag me, state
  358.   set pHiliteFlag to state
  359. end
  360.